﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Configuration;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.Model;

namespace FantastPlatform.Community.UI.Service
{
    public class PersonSearchTools
    {

        /// <summary>
        /// 基础字符串定义
        /// </summary>
        static string InfoStr = "&F_SFZH=";
        static string qkljStr = "&tabId=qklj&params=";
        public static IPerson GetPersonInfoByIdCard(string idCard)
        {
            try
            {

                string changeIdCard = "";
                string baseInfo = "";
                IPerson person = null;
                if (idCard != "")
                {
                    //根据身份证位数对其进行加密处理
                    if (idCard.Length == 18)
                    {
                        changeIdCard = GetChangeIdCard(idCard);
                    }
                    else if (idCard.Length == 15)
                    {
                        changeIdCard = GetChangeIdCard1(idCard);
                    }
                    if (changeIdCard != "")
                    {
                        baseInfo = BaseInfo(changeIdCard);
                        if (!string.IsNullOrEmpty(baseInfo))
                        {

                            //  baseInfo = "{" + baseInfo + "}";
                            person = new Person();
                            List<string> propertyList = baseInfo.Split(',').ToList();
                            foreach (string item in propertyList)
                            {
                                string[] propertyArry = item.Split(':');
                                if (propertyArry.Length > 1)
                                {
                                    switch (propertyArry[0])
                                    {
                                        case "\"身份证号\"":
                                            person.IdCard = propertyArry[1].Substring(1, propertyArry[1].Length - 2);
                                            break;
                                        case "\"姓名\"":
                                            person.Name = propertyArry[1].Substring(1, propertyArry[1].Length - 2);
                                            break;
                                        case "\"性别\"":
                                            person.Sex = propertyArry[1].Substring(1, propertyArry[1].Length - 3);
                                            break;
                                        case "\"民族\"":
                                            person.Nation = propertyArry[1].Substring(1, propertyArry[1].Length - 2);
                                            break;                                      
                                        case "\"籍贯省市县区\"":
                                            person.NativePlace = propertyArry[1].Substring(1, propertyArry[1].Length - 2);
                                            break;
                                        case "\"住址详址\"":
                                            person.RegisterAddress = propertyArry[1].Substring(1, propertyArry[1].Length - 2);
                                            break;
                                    }

                                }
                            }
                        }
                    }
                }
                return person;
            }
            catch (Exception)
            { return null; }
        }
        /// <summary>
        /// 18位身份证字符处理
        /// </summary>
        /// <param name="idcard"></param>
        /// <returns></returns>
        private static string GetChangeIdCard(string idcard)
        {
            string changeIdcard = "";
            WebClient wc = new WebClient();
            byte[] PersonResponse = wc.DownloadData(ConfigurationManager.AppSettings["resultInfo"] + InfoStr + idcard);

            string sss = System.Text.Encoding.GetEncoding("utf-8").GetString(PersonResponse);

            string mark = "eyJTRlpII";

            if (sss.Contains(mark))
            {
                int index = sss.IndexOf(mark);
                changeIdcard = sss.Substring(index, 40);
            }

            return changeIdcard;
        }

        /// <summary>
        /// 15位身份证字符处理
        /// </summary>
        /// <param name="idcard"></param>
        /// <returns></returns>
        private static string GetChangeIdCard1(string idcard)
        {
            string changeIdcard = "";
            WebClient wc = new WebClient();
            byte[] PersonResponse = wc.DownloadData(ConfigurationManager.AppSettings["resultInfo"] + InfoStr + idcard);

            string sss = System.Text.Encoding.GetEncoding("utf-8").GetString(PersonResponse);

            string mark = "eyJT";

            if (sss.Contains(mark))
            {
                int index = sss.IndexOf(mark);
                changeIdcard = sss.Substring(index, 36);
            }

            return changeIdcard;
        }

        /// <summary>
        /// 根据加密身份证字符，得到详细基本信息
        /// </summary>
        /// <param name="IdCardInfo"></param>
        /// <returns></returns>
        private static string BaseInfo(string IdCardInfo)
        {
            string BaseInfo = "";
            WebClient wbc = new WebClient();
            byte[] baseInfo = wbc.DownloadData(ConfigurationManager.AppSettings["baseInfo"] + "&params=" + IdCardInfo);
            string baseInfoStr = System.Text.Encoding.GetEncoding("utf-8").GetString(baseInfo);

            BaseInfo = GetBaseIndo(baseInfoStr);
            return BaseInfo;

        }

        /// <summary>
        /// 根据加密身份证字符，得到详细犯罪信息
        /// </summary>
        /// <param name="IdCardInfo"></param>
        /// <returns></returns>
        private static string CrimInfo(string IdCardInfo)
        {
            string crimInfo = "";
            WebClient wbc = new WebClient();
            byte[] PersonResponse = wbc.DownloadData(ConfigurationManager.AppSettings["detailInfo"] + qkljStr + IdCardInfo);
            string detailInfoStr = System.Text.Encoding.GetEncoding("utf-8").GetString(PersonResponse);

            crimInfo = GetCrimInfo(detailInfoStr);
            return crimInfo;
        }

        /// <summary>
        /// 处理返回的字符串，返回犯罪信息
        /// </summary>
        /// <param name="messageInfo"></param>
        /// <returns></returns>
        private static string GetCrimInfo(string messageInfo)
        {
            string personInfo = string.Empty;
            try
            {
                #region 处理返回的字符串
                string mark1 = "QueryWFFZ";
                string mark2 = "QueryQGZT";
                string mark3 = "QueryXDRY";
                if (messageInfo != "")
                {
                    if (messageInfo.Contains(mark1))
                    {
                        //违法犯罪
                        int WFFZ = messageInfo.IndexOf(mark1);
                        string ss = messageInfo.Substring(WFFZ, 13);
                        string str = ss.Substring(ss.Length - 2, 2);
                        if (str != "[]")
                        {
                            personInfo += "该人员是违法犯罪人员！";
                        }
                    }
                    if (messageInfo.Contains(mark2))
                    {
                        //全国在逃
                        int QGZT = messageInfo.IndexOf(mark2);
                        string ss1 = messageInfo.Substring(QGZT, 13);
                        string str1 = ss1.Substring(ss1.Length - 2, 2);
                        if (str1 != "[]")
                        {
                            personInfo = "该人员是全国在逃人员！";
                        }
                    }
                    if (messageInfo.Contains(mark3))
                    {
                        //吸毒人员
                        int XDRY = messageInfo.IndexOf(mark3);
                        string ss2 = messageInfo.Substring(XDRY, 13);
                        string str1 = ss2.Substring(ss2.Length - 2, 2);
                        if (str1 != "[]")
                        {
                            personInfo = "该人员是吸毒人员！";
                        }
                    }
                }
                if (personInfo == "")
                {
                    personInfo = "该人员没有任何不良信息！";
                }
                #endregion
            }
            catch (Exception ex)
            {

            }
            return personInfo;
        }

        /// <summary>
        /// 处理返回的字符串，返回基本信息
        /// </summary>
        /// <param name="baseInfo"></param>
        /// <returns></returns>
        private static string GetBaseIndo(string baseInfo)
        {
            string BaseInfoo = string.Empty;
            try
            {
                #region 处理返回的字符串
                string markH = "{";
                string markE = "}";
                if (baseInfo.Length > 0)
                {
                    if (baseInfo.Contains(markH) || baseInfo.Contains(markE))
                    {
                        int Hcount = baseInfo.IndexOf(markH);
                        int Ecount = baseInfo.LastIndexOf(markE);
                        BaseInfoo = baseInfo.Substring(Hcount, Ecount);

                        BaseInfoo = BaseInfoo.Replace("SFZH", "身份证号");
                        BaseInfoo = BaseInfoo.Replace("XM", "姓名");
                        BaseInfoo = BaseInfoo.Replace("CYM", "曾用名");
                        BaseInfoo = BaseInfoo.Replace("CSRQ", "出生日期");
                        BaseInfoo = BaseInfoo.Replace("XB", "性别");
                        BaseInfoo = BaseInfoo.Replace("SG", "身高");
                        BaseInfoo = BaseInfoo.Replace("MZ", "民族");
                        BaseInfoo = BaseInfoo.Replace("CSD", "出生地");
                        BaseInfoo = BaseInfoo.Replace("CSDXZ", "出生地详址");
                        BaseInfoo = BaseInfoo.Replace("JGSSX", "籍贯省市县区");
                        BaseInfoo = BaseInfoo.Replace("WHCD", "文化程度");
                        BaseInfoo = BaseInfoo.Replace("HYZK", "婚姻状况");
                        BaseInfoo = BaseInfoo.Replace("BYQK", "兵役情况");
                        BaseInfoo = BaseInfoo.Replace("HH", "户号");
                        BaseInfoo = BaseInfoo.Replace("YHZGX", "与户主关系");
                        BaseInfoo = BaseInfoo.Replace("ZZSSXQ", "住址省市县(区)");
                        BaseInfoo = BaseInfoo.Replace("ZZXZ", "住址详址");
                        BaseInfoo = BaseInfoo.Replace("HDQR", "何地迁入");
                        BaseInfoo = BaseInfoo.Replace("QTZZSSXQ", "其他住址省市县(区)");
                        BaseInfoo = BaseInfoo.Replace("QTZZXZ", "其他住址详址");
                        BaseInfoo = BaseInfoo.Replace("BDRQ", "变动日期");
                        BaseInfoo = BaseInfoo.Replace("BDYY", "变动原因");
                        BaseInfoo = BaseInfoo.Replace("ZY", "职业");
                        BaseInfoo = BaseInfoo.Replace("FWCS", "服务处所");
                        BaseInfoo = BaseInfoo.Replace("{", "");
                        BaseInfoo = BaseInfoo.Replace("}]);", "");
                        BaseInfoo = BaseInfoo.Replace("</script>", "");
                        BaseInfoo = BaseInfoo.Trim();
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
            }
            return BaseInfoo;
        }
    }

}